/*
 * UIStyle.cpp
 *
 *  Created on: 2010-3-19
 *      Author: Administrator
 */

#include "UIStyle.h"
#include <e32std.h>
#include <e32cmn.h>
#include <e32const.h>
#include "TSimpleDelimiedParser.h"

void UIFont::CreateFont(const TDesC& fontName,TInt fontsize)
	{
	
	}

UILength::UILength()
	{
	iLength = 0;
	iUnit = ELU_Auto;
	}

UILength::UILength(TInt aVal,ELengthUnit aUnit)
	{
	iLength = aVal;
	iUnit = aUnit;
	}

void UILength::SetLength(TInt aVal,ELengthUnit aUnit)
	{
	iLength = aVal;
	iUnit = aUnit;
	}

UILength& UILength::operator=(const TDesC& aLength)
	{
	if(aLength.Right(1) == _L("%"))
		{
		TLex lex(aLength.Left(aLength.Length()-1));
		lex.Val(iLength);
		iUnit = ELU_Percent;
		}
	else if(aLength.Mid(aLength.Length()-2,2) == _L("px"))
		{
		TLex lex(aLength.Left(aLength.Length()-2));
		lex.Val(iLength);
		iUnit = ELU_Pixel;
		}
	else
		{
		iLength = 0;
		iUnit = ELU_Unknown;
		}
	
	return *this;
	}

TInt UILength::GetLength(TInt aMaxLength) const
	{
	if(iUnit == ELU_Percent)
		{
		if(aMaxLength >= 0)
			return aMaxLength*iLength/100;
		else if(aMaxLength == -1)
			return 0;
		}
	else if(iUnit == ELU_Pixel)
		return iLength;
	else if(iUnit == ELU_Auto)
		return 0;
	else 
		return 0;
	
	return 0;
	}

TInt UILength::GetUnit() const
	{
	return iUnit;
	}

void UIMargin::AssignL(const TDesC& aMargin)
	{
	TLex lex(aMargin);
	TInt flag = 0;
	RPointerArray<HBufC> marginArray;
	
	while(flag < 4)
		{
		TPtrC ptr = lex.NextToken();
		
		if(ptr ==_L(""))
			break;
		
		HBufC* ptemp = HBufC::NewL(ptr.Length());
		CleanupStack::PushL(ptemp);
		
		TPtr ptrTemp = ptemp->Des();
		ptrTemp.Copy(ptr);
		
		marginArray.AppendL(ptemp);
		
		CleanupStack::Pop(ptemp);
		
		flag++;
		}
	
	if(flag == 1)
		{
		iLeft = *marginArray[0];
		iTop = *marginArray[0];
		iRight = *marginArray[0];
		iBottom = *marginArray[0];
		}
	else if(flag == 2)
		{
		iTop = *marginArray[0];
		iBottom = *marginArray[0];
		iRight = *marginArray[1];
		iLeft = *marginArray[1];
		}
	else if(flag == 3)
		{
		iTop = *marginArray[0];
		iBottom = *marginArray[0];
		iRight = *marginArray[1];
		iLeft = *marginArray[2];
		}
	else
		{
		iTop = *marginArray[0];
		iBottom = *marginArray[1];
		iRight = *marginArray[2];
		iLeft = *marginArray[3];	
		}
	
	
	marginArray.ResetAndDestroy();
	}

TInt UIMargin::GetLeft(TInt aMax) const
	{
	return iLeft.GetLength(aMax);
	}

TInt UIMargin::GetTop(TInt aMax) const
	{
	return iTop.GetLength(aMax);
	}
TInt UIMargin::GetRight(TInt aMax) const
	{
	return iRight.GetLength(aMax);
	}
TInt UIMargin::GetBottom(TInt aMax) const
	{
	return iBottom.GetLength(aMax);
	}

UIColor& UIColor::operator=(const TDesC& aColor)
	{
	if(aColor.Left(1) != _L("#"))
		return *this;
	if(aColor.Length() != 7 && aColor.Length() != 9)
		return *this;
	
	TLex lex(aColor.Mid(1,2));
	TUint32 red = 0;
	lex.Val(red,EHex);
	
	lex.Assign(aColor.Mid(3,2));
	TUint32 green = 0;
	lex.Val(green,EHex);
	
	lex.Assign(aColor.Mid(5,2));
	TUint32 blue = 0;
	lex.Val(blue,EHex);
	
	
	TUint32 alpha = 255;
	if(aColor.Length() == 9)
		{
		lex.Assign(aColor.Mid(7,2));
		lex.Val(alpha,EHex);
		}
	
	
	iColor.SetRed(red);
	iColor.SetGreen(green);
	iColor.SetBlue(blue);
	iColor.SetAlpha(alpha);
	
	return *this;
	}

TRgb UIColor::GetColor() const
	{
	return iColor;
	}

UISingleBorder& UISingleBorder::operator=(const TDesC& aDes)
	{
	TSimpleDelimiedParser parser;
	parser.ParserWithDelimiter(aDes,' ');
	
	
	if(parser.HasSegment())
		{
		TInt flag = 0;
		
		while(1)
			{
			if(parser.IsEos())
				break;
			
			TPtrC ptr = parser.NextToken();
			
			if(flag == 0)
				iWidth = ptr;
			else if(flag == 1)
				{
				if(ptr == _L("solid"))
					iBorderStyle = EBStyle_Solid;
				else if(ptr == _L("dash"))
					iBorderStyle = EBStyle_Dash;
				else if(ptr == _L("dot"))
					iBorderStyle = EBStyle_Dot;
				else
					iBorderStyle = EBStyle_Solid;
				}
			else if(flag == 2)
				iColor = ptr;
			
			flag++;
			
			if(flag >= 3)
				break;
			}
		}
	
	return *this;
	}

TBool UIBorder::IsSet(EBorderSet aSet)
	{
	return iMask.IsSet(aSet);
	}

void UIBorder::Set(EBorderSet aSet)
	{
	iMask.Set(aSet);
	}

UIStyle::UIStyle()
	{
	// TODO Auto-generated constructor stub

	}

UIStyle::~UIStyle()
	{
	// TODO Auto-generated destructor stub
	}
